<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Han">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/try6.gitee.io/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/try6.gitee.io/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/try6.gitee.io/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/try6.gitee.io/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/try6.gitee.io/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/try6.gitee.io/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/try6.gitee.io/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="其实工作中一直都有异步流程控制的需求，主要是各种需要顺序执行的AJAX。 之前请求不多的时候，我就直接简单粗暴地在回调里调用下一个请求函数，随着项目越来越大，越来越复杂，这种方式难以维护的缺点就越发的明显。大热的ES6中的promise方法的确很强，然而，需要兼容IE7（现在是IE10了）的我流下了两行清泪….而且我们项目还未引入打包和配置工具（没错就是这么落后），所以没有办法愉快地使用Promi">
<meta property="og:type" content="article">
<meta property="og:title" content="兼容IE大佬的异步流程控制大法">
<meta property="og:url" content="http://try66.gitee.io/try6.gitee.io/2018/02/10/2018-02-27兼容IE大佬的异步流程控制大法/index.html">
<meta property="og:site_name" content="Hello JS">
<meta property="og:description" content="其实工作中一直都有异步流程控制的需求，主要是各种需要顺序执行的AJAX。 之前请求不多的时候，我就直接简单粗暴地在回调里调用下一个请求函数，随着项目越来越大，越来越复杂，这种方式难以维护的缺点就越发的明显。大热的ES6中的promise方法的确很强，然而，需要兼容IE7（现在是IE10了）的我流下了两行清泪….而且我们项目还未引入打包和配置工具（没错就是这么落后），所以没有办法愉快地使用Promi">
<meta property="og:locale" content="zh-Han">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2865721-f264d38ff4fa2614?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2865721-bc34b37da5dbcd81?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-02T00:26:38.608Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="兼容IE大佬的异步流程控制大法">
<meta name="twitter:description" content="其实工作中一直都有异步流程控制的需求，主要是各种需要顺序执行的AJAX。 之前请求不多的时候，我就直接简单粗暴地在回调里调用下一个请求函数，随着项目越来越大，越来越复杂，这种方式难以维护的缺点就越发的明显。大热的ES6中的promise方法的确很强，然而，需要兼容IE7（现在是IE10了）的我流下了两行清泪….而且我们项目还未引入打包和配置工具（没错就是这么落后），所以没有办法愉快地使用Promi">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/2865721-f264d38ff4fa2614?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/try6.gitee.io/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://try66.gitee.io/try6.gitee.io/2018/02/10/2018-02-27兼容IE大佬的异步流程控制大法/"/>





  <title>兼容IE大佬的异步流程控制大法 | Hello JS</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?cc7e3057efb482b0f56c29d2716384da";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Han">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/try6.gitee.io/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Hello JS</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">所有的安排都是最好的</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/try6.gitee.io/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/try6.gitee.io/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://try66.gitee.io/try6.gitee.io/try6.gitee.io/2018/02/10/2018-02-27兼容IE大佬的异步流程控制大法/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Try_Luies">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/try6.gitee.io/images/pic.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hello JS">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">兼容IE大佬的异步流程控制大法</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-02-10T05:48:46+08:00">
                2018-02-10
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/try6.gitee.io/2018/02/10/2018-02-27兼容IE大佬的异步流程控制大法/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2018/02/10/2018-02-27兼容IE大佬的异步流程控制大法/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/try6.gitee.io/2018/02/10/2018-02-27兼容IE大佬的异步流程控制大法/" class="leancloud_visitors" data-flag-title="兼容IE大佬的异步流程控制大法">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">Visitors&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>其实工作中一直都有异步流程控制的需求，主要是各种需要顺序执行的AJAX。</p>
<p>之前请求不多的时候，我就直接简单粗暴地在回调里调用下一个请求函数，随着项目越来越大，越来越复杂，这种方式难以维护的缺点就越发的明显。大热的ES6中的promise方法的确很强，然而，需要兼容IE7（现在是IE10了）的我流下了两行清泪….而且我们项目还未引入打包和配置工具（没错就是这么落后），所以没有办法愉快地使用Promise等ES6语法和对象。</p>
<p>这个问题一直非常困扰我，想了很久都没有比较好的解决办法，直到有一天，看到了一个CSS3动画的制作，依赖JQ，控制异步动画的时候完全使用了deferred对象，仿佛打开了异步流程新世界的大门</p>
<a id="more"></a>
<h3 id="deferred对象的使用方法"><a href="#deferred对象的使用方法" class="headerlink" title="deferred对象的使用方法"></a>deferred对象的使用方法</h3><ol>
<li>创建Deferred对象</li>
<li>在异步流程结束后改变deferred对象状态</li>
<li>返回deferred对象<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">function F()&#123;</span><br><span class="line">    var def = $.Deferred();  //创建deferred对象</span><br><span class="line">    setTimeout(function()&#123;</span><br><span class="line">        console.log(&quot;hi~&quot;);</span><br><span class="line">        def.resolve();    //改变deferred对象状态为“已完成”</span><br><span class="line">    &#125;,50);</span><br><span class="line">    return def; //返回deferred对象</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>然后在需要的地方调用这个异步函数，使用<code>then</code>表示该方法执行完后再执行下一步<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">F().then(function()&#123;</span><br><span class="line">    //do something you want</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure></p>
<p>恩，妈妈再也不用担心我的异步了，即便是有多个异步回调需要控制，也能使用连续的<code>then</code>写出漂亮的顺序流<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">F1().then(function()&#123;</span><br><span class="line">    F2();</span><br><span class="line">&#125;).then(function()&#123;</span><br><span class="line">    F3();</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure></p>
<p>似乎回调依赖的问题已经解决了，直到有一天，我遇到了一个循环异步回调</p>
<h3 id="一个流程依赖多个异步回调"><a href="#一个流程依赖多个异步回调" class="headerlink" title="一个流程依赖多个异步回调"></a>一个流程依赖多个异步回调</h3><p>大致需求是，有一个循环体，里面的循环内容是异步流程，大概可以写成这样<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">for(var i = 0 ;i &lt; 10 ; i++)&#123;</span><br><span class="line">       setTimeout(function()&#123;</span><br><span class="line">           console.log(&quot;我要吃饭啦~&quot;+i);</span><br><span class="line">       &#125;);</span><br><span class="line">   &#125;</span><br><span class="line">console.log(&quot;我吃饱啦~&quot;)</span><br></pre></td></tr></table></figure></p>
<p><code>promise</code>对象里有个<code>all</code>，可以用放入数组的形式控制多个异步依赖，没有编译工具又要保证兼容性的我再次留下两行清泪。。研究了半天，好像没有在Deferred对象中发现有类似的方法。</p>
<p>于是先是尝试着，还是使用单个流程控制的方法去写<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">function wait()&#123;</span><br><span class="line">    var deferred = $.Deferred();</span><br><span class="line">     for(var i = 0 ;i &lt; 10 ; i++)&#123;</span><br><span class="line">        setTimeout(function()&#123;</span><br><span class="line">            console.log(&quot;我要吃饭啦~&quot;+i); </span><br><span class="line">            deferred.resolve();</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">    return deferred;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">wait().then(function()&#123;</span><br><span class="line">    console.log(&quot;我吃饱啦~&quot;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></p>
<p>emmmmm当然这是不对的了，循环第一次的时候，deferred状态就已经更改为完成，所以就会执行“我吃饱啦”，然后再继续执行剩余的9次。</p>
<p><img src="http://upload-images.jianshu.io/upload_images/2865721-f264d38ff4fa2614?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>于是，我做了如下改写，使用变量count作为计数标识，当计数循环完成之后，再改变deferred对象的状态。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">function wait()&#123;</span><br><span class="line">  var dtd = $.Deferred()</span><br><span class="line">   var count = 0;</span><br><span class="line">     for(var i = 0 ;i &lt; 10 ; i++)&#123;</span><br><span class="line">        setTimeout(function()&#123;</span><br><span class="line">            console.log(&quot;我要吃饭啦~&quot;+i); </span><br><span class="line">            count++;</span><br><span class="line">            if( count == 10 )&#123;</span><br><span class="line">                 dtd.resolve();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,500);</span><br><span class="line">    &#125;</span><br><span class="line">    return dtd;</span><br><span class="line">&#125;</span><br><span class="line">wait().done(function()&#123;console.log(&quot;我吃饱啦&quot;)&#125;)</span><br></pre></td></tr></table></figure></p>
<p>运行10次“我要吃饭啦”，随后运行“我吃饱啦”</p>
<p><img src="http://upload-images.jianshu.io/upload_images/2865721-bc34b37da5dbcd81?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>成功啦~给自己一朵小红花！</p>
<h4 id="兼容性测试"><a href="#兼容性测试" class="headerlink" title="兼容性测试"></a>兼容性测试</h4><p>最后的最后，在IE的各个环境下进行测试，居然在IE5下也能跑。所以只要是能支持JQ的Deferred对象，一般情况下这种异步流程操作方式都是可行的。</p>
<p><code>ps</code>：感觉自己是走了歪门邪道，不知道<code>deferred</code>对象中是不是有类似于<code>promise</code>对象中的<code>all</code>可以直接控制多个异步，如果有的话，还希望各位大大能指点迷津</p>
<p><code>pss</code>：关于公司还是使用非常传统开发方式的问题，只能说上有政策的阻碍，下有业务的局限。原因很复杂，一时半会儿也说不清，所以只能找机会跑路啦哈哈哈哈</p>
<p>学习资料：<br><a href="http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object" target="_blank" rel="noopener">jQuery的deferred对象详解——阮一峰</a></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>Donate</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.jpg" alt="Try_Luies WeChat Pay"/>
        <p>WeChat Pay</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpg" alt="Try_Luies Alipay"/>
        <p>Alipay</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/try6.gitee.io/2018/02/08/2018-02-08电子地图需求文档/" rel="next" title="电子地图需求文档">
                <i class="fa fa-chevron-left"></i> 电子地图需求文档
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/try6.gitee.io/2018/03/05/2018-03-01一年前端面试打怪升级之路（三）/" rel="prev" title="一年前端面试打怪升级之路（三）">
                一年前端面试打怪升级之路（三） <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/try6.gitee.io/images/pic.jpg"
                alt="Try_Luies" />
            
              <p class="site-author-name" itemprop="name">Try_Luies</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/try6.gitee.io/archives/">
              
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/try6" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-GitHub"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/7a6944daafc3" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-globe"></i>简书</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="www.baidu.com" target="_blank" title="微博">
                      
                        <i class="fa fa-fw fa-微博"></i>微博</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#deferred对象的使用方法"><span class="nav-number">1.</span> <span class="nav-text">deferred对象的使用方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#一个流程依赖多个异步回调"><span class="nav-number">2.</span> <span class="nav-text">一个流程依赖多个异步回调</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#兼容性测试"><span class="nav-number">2.1.</span> <span class="nav-text">兼容性测试</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Try_Luies</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/try6.gitee.io/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/try6.gitee.io/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/try6.gitee.io/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/try6.gitee.io/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/try6.gitee.io/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/try6.gitee.io/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/try6.gitee.io/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/try6.gitee.io/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/try6.gitee.io/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/try6.gitee.io/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/try6.gitee.io/js/src/bootstrap.js?v=5.1.4"></script>



  


  

    
      <script id="dsq-count-scr" src="https://try.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://try66.gitee.io/try6.gitee.io/2018/02/10/2018-02-27兼容IE大佬的异步流程控制大法/';
          this.page.identifier = '2018/02/10/2018-02-27兼容IE大佬的异步流程控制大法/';
          this.page.title = '兼容IE大佬的异步流程控制大法';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://try.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("", "");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  

  

  

  

</body>
</html>
